<?php

require_once('../includes/SmartyManager.class.php');
require_once('../includes/DB.class.php');
require_once('../includes/Filter.class.php');
require_once('../includes/Verifier.class.php');
require_once('../includes/exceptions/AppExceptions.class.php');
require_once('../includes/functions.inc.php');

$id = $_POST['id'];
$type = $_POST['type'];
$smarty = SmartyManager::getSmarty();
try{
$certificationNumber = $_POST['certificationNumber'];
$certificationCodification = $_POST['certificationCodification'];
$consignmentBatch = Filter::forDBInsertion($_POST['consignmentBatch']);
	
beginTransaction();

if($type == 'main'){
	$size1 = $_POST['size1'];
	if(!Verifier::isNUMBER($size1)){
		throw new CustomException('厚度应该为数字，您填写的这个值：'.$size1.'不合格');
	}
	$size2 = $_POST['size2'];
	if(!Verifier::isNUMBER($size2)){
		throw new CustomException('长度应该为数字，您填写的这个值：'.$size2.'不合格');
	}
	$size3 = $_POST['size3'];
	if(!Verifier::isNUMBER($size3)){
		throw new CustomException('宽度应该为数字，您填写的这个值：'.$size3.'不合格');
	}
	
	$count = $_POST['count'];
	if(!Verifier::isNUMBER($count)){
		throw new CustomException('数量应该为数字，您填写的这个值：'.$count.'不合格');
	}
	
	$unitWeight = $_POST['unitWeight'];
	if(!Verifier::isNUMBER($unitWeight)){
		throw new CustomException('单重应该为数字，您填写的这个值：'.$unitWeight.'不合格');
	}
	
	$unitPrice = $_POST['unitPrice'];
	if( !empty($unitPrice) && !Verifier::isNUMBER($unitPrice)){
		throw new CustomException('受订单价应该为数字，您填写的这个值：'.$unitPrice.'不合格');
	}
	$facheNumber = $_POST['facheNumber'];
	$sequenceNumber = $_POST['sequenceNumber'];
	$manufactory = $_POST['manufactory'];
	$orderNumber = Filter::forDBInsertion($_POST['orderNumber']);
	$orderSubitemNumber = Filter::forDBInsertion($_POST['orderSubitemNumber']);
	$authenticationMark = Filter::forDBInsertion($_POST['authenticationMark']);
	$steelNumber = Filter::forDBInsertion($_POST['steelNumber']);
	$materialNumber = Filter::forDBInsertion($_POST['materialNumber']);
	$sampleBatch = Filter::forDBInsertion($_POST['sampleBatch']);
	$purchaseNumber = Filter::forDBInsertion($_POST['purchaseNumber']);
	$shipmentDate = Filter::forDBInsertion($_POST['shipmentDate']);
	$month = Filter::forDBInsertion($_POST['month']);
	$vehicleNumber = Filter::forDBInsertion($_POST['vehicleNumber']);
	$consignee = Filter::forDBInsertion($_POST['consignee']);
	$remark = Filter::forDBInsertion($_POST['remark']);
	
	$query = 'select * from little_main where id = '.$id;
	findAndLogEdit($query, $_POST);
	
	$query = "update little_main set certificationNumber = '$certificationNumber', facheNumber = '$facheNumber',  certificationCodification = '$certificationCodification',
		sequenceNumber = '$sequenceNumber', manufactory = '$manufactory', 	
		authenticationMark = '$authenticationMark', steelNumber = '$steelNumber', size1 = $size1, size2 = $size2, size3 = $size3, 
		count = $count, unitWeight = $unitWeight, orderNumber = '$orderNumber', orderSubitemNumber = '$orderSubitemNumber',
		unitPrice = '$unitPrice', materialNumber = '$materialNumber', consignmentBatch = '$consignmentBatch', 
		sampleBatch = '$sampleBatch',
		purchaseNumber = '$purchaseNumber', shipmentDate = '$shipmentDate', month = '$month',
		vehicleNumber = '$vehicleNumber', consignee = '$consignee', remark = '$remark'
		where id = $id";
}else if($type == 'ruku'){
	$rukuDate = $_POST['rukuDate'];
	if(!Verifier::isDATE($rukuDate)){
		throw new CustomException('发车时间格式不正确，请按要求输入发船时间');
	}
	$remark1 = $_POST['remark1'];
	$remark2 = $_POST['remark2'];
	
	$query = 'select * from little_ruku where id = '.$id;
	findAndLogEdit($query, $_POST);
	
	$query = "update little_ruku set rukuDate = '$rukuDate',certificationNumber = '$certificationNumber', certificationCodification = '$certificationCodification',
		consignmentBatch = '$consignmentBatch', remark1 = '$remark1', remark2 = '$remark2'
		where id = $id";
	
}else if($type == 'chuku'){
	$chukuDate = $_POST['chukuDate'];
	if(!Verifier::isDATE($chukuDate)){
		throw new CustomException('发船时间格式不正确，请按要求输入发船时间');
	}
	$handler = $_POST['handler'];
	$remark1 = $_POST['remark1'];
	$remark2 = $_POST['remark2'];
	
	$query = 'select * from little_chuku where id = '.$id;
	findAndLogEdit($query, $_POST);
	
	$query = "update little_chuku set chukuDate = '$chukuDate',certificationNumber = '$certificationNumber', certificationCodification = '$certificationCodification',
		consignmentBatch = '$consignmentBatch', remark1 = '$remark1', remark2 = '$remark2', handler = '$handler'
		where id = $id";
}

DB::query($query);
	commit();
}catch(Exception $e){
	$smarty->assign('errTitle', '操作错误');
	$smarty->assign('errMsg', $e);
	$smarty->display('error.html');
	rollback();
	die();
}

$smarty->assign('successTitle','修改成功');
$smarty->display('success.html');

?>